<?php

switch(Form::get('action')){

case 'Login':  
		login_traitement(); 
break;

case 'Deconnect' : 
		Session::Shutdown('membre'); 
		Site::redirect("index.php");
break;

default:

	if(Session::IsOpen('membre')){
		echo "<div id=\"barre-login2\">";
		echo "<b>Utilisateur : ".Session::Info('membre','pseudo')."</b>";
		echo "&nbsp;<a href='index.php?module=Login&amp;action=Deconnect'>Déconnexion</a>";
		echo "</div>";

	}else
		afficheformlogin();
}

function afficheformlogin() {
echo "<div id=\"bt-login\"> </div>";
echo "<div id=\"barre-login\">";
echo "<form action='index.php?module=Login&amp;action=Login' method='post'>
			<input type='text' name='pseudo' class='inpout_pseudo'/>
			<input type='password' name='mdp' class='inpout_mdp' />
			<input type='hidden' name='urlprec' value=\"".$_SERVER['QUERY_STRING']."\" />
			<input type='submit' value=' 'class='inpout_connexion'/>
		</form><a href='index.php?module=inscription' class='inscription'>Inscription</a>";
echo "</div>";
}

function login_traitement(){
	if( Form::get('pseudo')=='' || Form::get('mdp')==''){
		Site::message_info("Tous les champs obligatoires n'ont pas été saisis".print_r($_POST,true));
		Site::redirect("index.php");
	
	}else if(Membre::CheckPseudo(Form::get('pseudo')) == 2) {
		
		if(Membre::CheckHash(Form::get('pseudo'),Form::get('mdp')) == 0) {
			$user= new Membre();
			$user = Membre::SearchByPseudo(Form::get('pseudo'));
			
			Session::Open('membre',$user);
			
		
			Site::redirect('?'.html_entity_decode(Form::get('urlprec'))); //html_entity_decode permet d'inverser le htmlentities du Form::get

		}else {
			Site::message_info("Le mot de passe est incorrect ".print_r($_POST,true));	
			Site::redirect("index.php");
		}
		
	}else {
	
		switch(Membre::CheckPseudo(Form::get('pseudo'))) {
			case 1: 
			Site::message_info("Le pseudo est invalide.".print_r($_POST,true));
			break;
			case 0:
			Site::message_info("Le pseudo n'existe pas".print_r($_POST,true));
			break;
			case 3:
			Site::message_info("Le pseudo est trop long".print_r($_POST,true));
		}
		Site::redirect("index.php");
	
	
	}
}
?>